/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import models.Baskets;
import models.ItemBasket;

import models.Orders;
import models.Products;
import models.Tables;
import models.Users;
import org.primefaces.component.tabview.Tab;
import services.Impl.BasketServiceImpl;
import services.Impl.ItemBasketsServiceImpl;

import services.Impl.OrdersServiceImpl;
import services.Impl.TableServiceImpl;

/**
 *
 * @author amega
 */
@ManagedBean(name="orderBean")
@SessionScoped
public class OrderBean {
    
    public OrderBean(){}
    
    @EJB
    private OrdersServiceImpl ordersService;
    @EJB
    private BasketServiceImpl basketService;
    @EJB
    private ItemBasketsServiceImpl ibService;
    @EJB
    private TableServiceImpl tableService;

    
    private Orders curentOrder;
    
    private Baskets curentBaskets; 
    
    private Users curentUsers;
    
    private Date dateOrder;
    //private 
    private Collection<Tables> tmpBusyTables;
    private int count;
    private double tmpPrice;
    
    public void addProductsToOrder(ActionEvent event) {
        curentUsers  = (Users)event.getComponent().getAttributes().get("users");
        if (curentOrder == null) {
            initOrder();
            initBaskets();
            
        }
        System.out.print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        Products products = (Products)event.getComponent().getAttributes().get("products");
        System.out.print(products);
        ItemBasket ib = new ItemBasket();
        ib.setCount(count);
        ib.setIdBasket(curentBaskets);
        ib.setIdProduct(products);
        ibService.create(ib);
        curentBaskets.getItemBasketCollection().add(ib);
        
         
        
    }
    
    private String tablesId;
    
    public void addTablesToOrder(ActionEvent event) {
        curentUsers  = (Users)event.getComponent().getAttributes().get("users");
        
        tablesId = (String)event.getComponent().getAttributes().get("tablesId");
        
      
    }
    
    public String addTable() {
        if (curentOrder == null) {
            initOrder();
            initBaskets();            
        }
        
        Tables table = tableService.find(Long.valueOf(tablesId));  
        Collection<Tables> bt = this.getBysyTables();
        if (bt.contains(table)) {
            return "tableBusy";
        }
        curentOrder.getTablesCollection().add(table);
        table.getOrdersCollection().add(curentOrder);
        ordersService.edit(curentOrder);
        tableService.edit(table);
        return "";
    }

    public double getOrderPrice() {
        double price = 0;
        for (Object o : curentBaskets.getItemBasketCollection()) {
            ItemBasket ib = (ItemBasket)o;
            Products p = ib.getIdProduct();
            price = price+ib.getCount()*p.getPrice();
        }
        return price;
    } 
    
    
    private void initOrder(){
        curentOrder = new Orders();
        if (dateOrder == null) {
            dateOrder = new Date();
        }
        curentOrder.setDateTime(dateOrder);
        curentOrder.setIdUser(curentUsers);
        ordersService.create(curentOrder);
    }
    
    private void initBaskets(){
        curentBaskets = new Baskets();
        curentBaskets.setIdOrder(curentOrder);
        basketService.create(curentBaskets);
        curentOrder.getBasketsCollection().add(curentBaskets);
    }
    
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Baskets getCurentBaskets() {
        return curentBaskets;
    }

    public void setCurentBaskets(Baskets curentBaskets) {
        this.curentBaskets = curentBaskets;
    }

    public Orders getCurentOrder() {
        return curentOrder;
    }

    public void setCurentOrder(Orders curentOrder) {
        this.curentOrder = curentOrder;
    }

    public Users getCurentUsers() {
        return curentUsers;
    }

    public void setCurentUsers(Users curentUsers) {
        this.curentUsers = curentUsers;
    }

    public Date getDateOrder() {
        return dateOrder;
    }

    public void setDateOrder(Date dateOrder) {
        this.dateOrder = dateOrder;
    }
    
    public Collection<Tables> getBysyTables() {
        Collection<Tables> bustTables = new ArrayList<Tables>();   
        for (Object o:ordersService.getOrdersInDate(dateOrder)){
            Orders order = (Orders)o;           
            Object[] arr= order.getTablesCollection().toArray();
            for (int i = 0; i < arr.length; i++) {
                bustTables.add((Tables)arr[i]);
            }
        }
        tmpBusyTables = bustTables;
        
        return bustTables;
        
    }
    
    public Collection<String> showTables() {
        Object[] allTables = tableService.findAll().toArray();
        Collection<Tables> busyTables = this.tmpBusyTables;
        Collection<String> dataOutPut = new ArrayList<String>();
       
        for (int i = 0; i < allTables.length; i++) {
         
            
            if (busyTables.contains((Tables)allTables[i])) {                      
                dataOutPut.add(" <a  href=\"#\"><img class=\"table"+(i+1)+" src=\"/ors/faces/javax.faces.resource/img/"+(i+1)+".png\"/></a>");             
            } else {
                dataOutPut.add(" <a  href=\"#\"><img class=\"table"+(i+1)+" src=\"/ors/faces/javax.faces.resource/img/"+(i+1)+"o.png\"/></a>");
            }     
                    
        }
        System.out.print(dataOutPut);
        return dataOutPut;
    }
    
    public Collection<ItemBasket> getOrderProducts(Orders order) {
        Collection<ItemBasket> pr= new ArrayList<ItemBasket>();
        tmpPrice = 0;
        for (Baskets bs:order.getBasketsCollection()) {
           for (Object o : bs.getItemBasketCollection()) {
            tmpPrice = tmpPrice+((ItemBasket)o).getIdProduct().getPrice()*((ItemBasket)o).getCount();   
            pr.add((ItemBasket)o);          
            } 
        }
        return pr;  
    }
    
    public Collection<Tables>  getTablesOrder(Orders order) {
        Collection<Tables> t = new ArrayList<Tables>();
        Object[] o = order.getTablesCollection().toArray();
        for (int i = 0; i < o.length; i++) {
                t.add((Tables)o[i]);
            }   
        return t;
    }
    
    public void userLogout() {
        this.count = 0;
        this.curentUsers =null;
        this.curentBaskets = null;
        this.curentOrder = null;
        this.dateOrder = null;
        this.tablesId = null;
    }

    public double getTmpPrice() {
        return tmpPrice;
    }

    public void setTmpPrice(double tmpPrice) {
        this.tmpPrice = tmpPrice;
    }
    
    
}
